package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.exception.BusinessException;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> getAllUsers() {
        logger.info("获取所有用户");
        return userRepository.findAll();
    }

    @Override
    public User getUserById(Long id) {
        logger.info("根据ID获取用户: {}", id);
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public User createUser(User user) {
        logger.info("创建用户: {}", user.getUsername());
        // 业务逻辑示例：检查用户名是否已存在
        /*if (userRepository.findByUsername(user.getUsername()) != null) {
            throw new BusinessException("用户名已存在");
        }*/
        return userRepository.save(user);
    }
}